﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using IDML.BusinessObject;
using IDML.BusinessLogic;

namespace IDML.Web.Modules.Admin
{
    public partial class AddTestStructure : System.Web.UI.Page
    {
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dt = new DataTable();
            }
        }

        protected void ddlControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (int.Parse(ddlControl.SelectedValue) == 2)//Dropdwon
            {
                this.DropDown.Visible = true;
            }
            else
            {
                this.DropDown.Visible = false;
            }
        }

        protected void grvDropDown_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string ID = grvDropDown.DataKeys[e.RowIndex].Values["ID"].ToString();

            dt = new DataTable();
            dt = (DataTable)ViewState["myTable"];
            for (int i = dt.Rows.Count - 1; i >= 0; i--)
            {
                DataRow dr = dt.Rows[i];
                if (dr["ID"].ToString() == ID)
                    dr.Delete();
            }
            //Store dt in Viewstate
            ViewState["myTable"] = dt;


            dt = (DataTable)ViewState["myTable"];
            grvDropDown.DataSource = dt;
            grvDropDown.DataBind();

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            dt = new DataTable();

            if (ViewState["myTable"] == null)
            {
                dt.Columns.Add("ID", typeof(int));
                dt.Columns.Add("Value", typeof(string));
            }
            else
            {
                dt = (DataTable)ViewState["myTable"];
            }

            string value = txtValue.Text;
            int count = dt.Rows.Count + 1;
            dt.Rows.Add(count, value);
            grvDropDown.DataSource = dt;
            grvDropDown.DataBind();

            //Store dt in Viewstate
            ViewState["myTable"] = dt;

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                TestStructureBO _testStructureBO = new TestStructureBO();
                _testStructureBO.TestId = int.Parse(ddlTest.SelectedValue);
                _testStructureBO.TestStructureText = txtLableName.Text;
                _testStructureBO.Unit = txtUnit.Text;
                _testStructureBO.ControlId = int.Parse(ddlControl.SelectedValue);
                _testStructureBO.ValidationId = int.Parse(ddlValidation.SelectedValue);
                _testStructureBO.TextOrder = int.Parse(txtOrder.Text);

                if (btnSave.Text == "Save" )
                {
                    dt = (DataTable)ViewState["myTable"];
                    TestBL _testBL = new TestBL();
                    int TestStructureId = _testBL.TestStructure_Insert(_testStructureBO, dt);
                    LoadTestStructuregrid();
                }
                else
                {
                    _testStructureBO.TestStructureId = int.Parse(ViewState["TestStructureId"].ToString());
                    TestBL _testBL = new TestBL();
                    _testBL.TestStructure_Update(_testStructureBO);
                    LoadTestStructuregrid();
                    btnSave.Text = "Save";

                }

                Clearcontrols();
            }
            catch (Exception)
            {
                throw;
            }

        }

        private void Clearcontrols()
        {
            txtLableName.Text = null;
            txtValue.Text = null;
            txtUnit.Text = null;
            txtOrder.Text = null;
            ddlControl.SelectedIndex=-1;
            ddlValidation.SelectedIndex = -1; 
        }

        protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadTestStructuregrid();
        }

        private void LoadTestStructuregrid()
        {
            TestBO _testBO = new TestBO();
            TestBL _testBL = new TestBL();
            _testBO.TestId = int.Parse(ddlTest.SelectedValue);
            GWTestStructure.DataSource = _testBL.GetTestStructure_Listst(_testBO);
            GWTestStructure.DataBind();
        }

        protected void GWTestStructure_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int testStructureId = int.Parse(GWTestStructure.DataKeys[0].Values[0].ToString());
            int isReference = 0;
            TestStructureBO testStructureBO = new TestStructureBO();
            TestBL _testBL = new TestBL();
            testStructureBO.TestStructureId = testStructureId;
            isReference = _testBL.TestStructure_Delete(testStructureBO);
            LoadTestStructuregrid();
        }

        //protected void GWTestStructure_RowEditing(object sender, GridViewEditEventArgs e)
        //{
        //    int testStructureId = int.Parse(GWTestStructure.DataKeys[0].Values[0].ToString());
        //    TestStructureBO testStructureBO = new TestStructureBO();
        //    TestBL _testBL = new TestBL();
        //    testStructureBO.TestStructureId = testStructureId;
        //   testStructureBO= _testBL.TestStructure_Select(testStructureBO);
        //   ddlControl.SelectedValue = testStructureBO.ControlId.ToString();
        //   txtLableName.Text = testStructureBO.TestStructureText;
        //   txtUnit.Text = testStructureBO.Unit;
        //   ddlValidation.SelectedValue = testStructureBO.ValidationId.ToString();
        //   txtOrder.Text = testStructureBO.TextOrder.ToString();

        //}

        protected void GWTestStructure_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = GWTestStructure.SelectedIndex;

            int testStructureId = int.Parse(GWTestStructure.DataKeys[index].Value.ToString());
            ViewState.Add("TestStructureId", testStructureId);
            // int testStructureId = int.Parse(GWTestStructure.SelectedRow.Cells[1].ToString());//int.Parse(GWTestStructure.DataKeys[e.].Values[0].ToString());

            TestStructureBO testStructureBO = new TestStructureBO();
            TestBL _testBL = new TestBL();
            testStructureBO.TestStructureId = testStructureId;
            testStructureBO = _testBL.TestStructure_Select(testStructureBO);
            ddlControl.SelectedValue = testStructureBO.ControlId.ToString();
            txtLableName.Text = testStructureBO.TestStructureText;
            txtUnit.Text = testStructureBO.Unit;
            ddlValidation.SelectedValue = testStructureBO.ValidationId.ToString();
            txtOrder.Text = testStructureBO.TextOrder.ToString();

            //ddlControl.SelectedValue=GWTestStructure.SelectedRow.Cells[1].Text;
            //txtLableName.Text = GWTestStructure.SelectedRow.Cells[0].Text;
            //txtUnit.Text = GWTestStructure.SelectedRow.Cells[2].Text;
            //ddlValidation.SelectedValue = GWTestStructure.SelectedRow.Cells[4].Text;
            //txtOrder.Text = GWTestStructure.SelectedRow.Cells[5].Text;
            btnSave.Text = "Edit";

        }

    }
}